package GUILayer;
import java.awt.*;

import javax.swing.table.DefaultTableModel;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;

import ModelLayer.*;
import ControlLayer.*;

import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JTextPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.ArrayList;

import javax.swing.BoxLayout;

/**
 * Dette er klassen MainGUI.
 * 
 * @author Gruppe 4
 * @version 7/4 2014
 *
 */
public class MainGUI extends JFrame {
	//private Dimension d = new Dimension();
	private CustomerCtr customerCtr;
	private ProductCtr productCtr;
	private SalesCtr salesCtr;
	private JTextField textProductID;
	private JTextField textProductName;
	private JTextField textAmount;
	private JTextField textCustomerName;
	private JTextField textCustomerAdress;
	private JTextField textCustomerZip;
	private JTextField textCustomerCity;
	private JTextField textCustomerPhone;
	private JTextField textCustomerEmail;
	private JTextField textOrderID;
	private JTextField textOrderDate;
	private JTextField textOrderDeliveryDate;
	private JTextField textOrderStatus;
	private Component createTableFrame;

	private JTable table;
	
	DefaultTableModel model;
	String col[] = {"Product ID","Name","Type","Sales Price","Stock","Country of Origin","Rent Price","Purchase Price","Minimum Stock"};
	
	
	/**
	 * Dette er konstruktoren for klassen MainGUI.
	 * 
	 * @param args
	 */
	public static void main(String[] args)
	{
		new MainGUI();
	}
	
	/**
	 * Denne metode laver et interface for klassen MainGUI.
	 */
	public MainGUI() {
		setVisible(true);
		setResizable(false);
		productCtr = ProductCtr.getInstance();

		//Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		//setLocation((int)d.getWidth()/3,(int)d.getHeight()/3);
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.NORTH);
		
		JPanel Sales = new JPanel();
		tabbedPane.addTab("Sales  ", null, Sales, null);
		
		JLabel lblProductId = new JLabel("Product ID:");
		
		textProductID = new JTextField();
		textProductID.setColumns(10);
		
		JButton btnProductSearch = new JButton("ProductSearch");
		
		JLabel lblProductName = new JLabel("Product Name:");
		
		textProductName = new JTextField();
		textProductName.setColumns(10);
		
		textAmount = new JTextField();
		textAmount.setColumns(10);
		
		JLabel lblAmount = new JLabel("Amount:");
		
		JButton btnAddProduct = new JButton("Add Product");
		
		JLabel lblSalesInformation = new JLabel("Sales Information");
		lblSalesInformation.setFont(new Font("Verdana", Font.BOLD, 12));
		
		JLabel lblOrderId = new JLabel("Order ID:");
		
		JLabel lblDate = new JLabel("Date:");
		
		JLabel lblDeliveryDate = new JLabel("Delivery Date:");
		
		JLabel lblTotal = new JLabel("Total:");
		
		JLabel lblDeliveryStatus = new JLabel("Delivery Status:");
		
		JLabel lblCompanyInformation = new JLabel("Company Information:");
		
		JLabel lblProducts = new JLabel("Products");
		lblProducts.setFont(new Font("Verdana", Font.BOLD, 12));
		
		JLabel lblCustomer = new JLabel("Customer");
		lblCustomer.setFont(new Font("Verdana", Font.BOLD, 12));
		
		JLabel lblName = new JLabel("Name:");
		
		JLabel lblAddress = new JLabel("Address:");
		
		JLabel lblZipCode = new JLabel("Zip Code:");
		
		JLabel lblCity = new JLabel("City:");
		
		JLabel lblPhoneNumber = new JLabel("Phone Number:");
		
		JLabel lblEmail = new JLabel("Email:");
		
		JButton btnAddCustomer = new JButton("Add Customer");
		
		textCustomerName = new JTextField();
		textCustomerName.setColumns(10);
		
		textCustomerAdress = new JTextField();
		textCustomerAdress.setColumns(10);
		
		textCustomerZip = new JTextField();
		textCustomerZip.setColumns(10);
		
		textCustomerCity = new JTextField();
		textCustomerCity.setColumns(10);
		
		textCustomerPhone = new JTextField();
		textCustomerPhone.setColumns(10);
		
		textCustomerEmail = new JTextField();
		textCustomerEmail.setColumns(10);
		
		textOrderID = new JTextField();
		textOrderID.setColumns(10);
		
		textOrderDate = new JTextField();
		textOrderDate.setColumns(10);
		
		textOrderDeliveryDate = new JTextField();
		textOrderDeliveryDate.setColumns(10);
		
		textOrderStatus = new JTextField();
		textOrderStatus.setColumns(10);
		
		JLabel labelTotal = new JLabel("");
		GroupLayout gl_Sales = new GroupLayout(Sales);
		gl_Sales.setHorizontalGroup(
			gl_Sales.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_Sales.createSequentialGroup()
					.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_Sales.createSequentialGroup()
							.addGap(75)
							.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_Sales.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(lblCompanyInformation))
								.addGroup(gl_Sales.createSequentialGroup()
									.addGap(296)
									.addComponent(lblCustomer, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_Sales.createSequentialGroup()
									.addGap(297)
									.addComponent(lblProducts))
								.addGroup(gl_Sales.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_Sales.createSequentialGroup()
											.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
												.addComponent(lblOrderId)
												.addComponent(lblDate)
												.addComponent(lblDeliveryDate)
												.addComponent(lblDeliveryStatus))
											.addGap(22)
											.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
												.addComponent(textOrderDeliveryDate, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
												.addComponent(textOrderDate, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
												.addComponent(textOrderID, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
												.addGroup(gl_Sales.createSequentialGroup()
													.addGroup(gl_Sales.createParallelGroup(Alignment.TRAILING)
														.addComponent(lblSalesInformation)
														.addGroup(gl_Sales.createSequentialGroup()
															.addComponent(textOrderStatus, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
															.addGap(36)
															.addComponent(lblTotal)))
													.addGap(18)
													.addComponent(labelTotal))))
										.addGroup(gl_Sales.createSequentialGroup()
											.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
												.addComponent(lblZipCode)
												.addComponent(lblCity)
												.addGroup(gl_Sales.createParallelGroup(Alignment.TRAILING)
													.addGroup(gl_Sales.createSequentialGroup()
														.addComponent(lblProductId)
														.addGap(47)
														.addComponent(textProductID, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE))
													.addGroup(gl_Sales.createSequentialGroup()
														.addComponent(btnProductSearch)
														.addGap(134))
													.addGroup(gl_Sales.createSequentialGroup()
														.addComponent(lblProductName)
														.addGap(31)
														.addComponent(textProductName, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE))))
											.addGap(45)
											.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_Sales.createSequentialGroup()
													.addComponent(lblAmount)
													.addGap(18)
													.addComponent(textAmount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addComponent(btnAddProduct)))
										.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING, false)
											.addGroup(gl_Sales.createSequentialGroup()
												.addComponent(lblEmail)
												.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(textCustomerEmail, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE))
											.addGroup(gl_Sales.createSequentialGroup()
												.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING)
													.addComponent(lblName)
													.addComponent(lblAddress))
												.addPreferredGap(ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
												.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING, false)
													.addComponent(textCustomerName, Alignment.TRAILING)
													.addComponent(textCustomerAdress, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)))
											.addGroup(gl_Sales.createSequentialGroup()
												.addComponent(lblPhoneNumber)
												.addGap(36)
												.addGroup(gl_Sales.createParallelGroup(Alignment.LEADING, false)
													.addComponent(textCustomerZip, Alignment.TRAILING)
													.addComponent(textCustomerCity, Alignment.TRAILING)
													.addComponent(textCustomerPhone, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE))))))))
						.addGroup(gl_Sales.createSequentialGroup()
							.addGap(75)
							.addComponent(btnAddCustomer)))
					.addGap(75))
		);
		gl_Sales.setVerticalGroup(
			gl_Sales.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_Sales.createSequentialGroup()
					.addComponent(lblSalesInformation)
					.addPreferredGap(ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblOrderId)
						.addComponent(textOrderID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDate)
						.addComponent(textOrderDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDeliveryDate)
						.addComponent(textOrderDeliveryDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDeliveryStatus)
						.addComponent(textOrderStatus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTotal)
						.addComponent(labelTotal))
					.addGap(18)
					.addComponent(lblCompanyInformation)
					.addGap(37)
					.addComponent(lblProducts, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblProductId)
						.addComponent(textProductID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblAmount)
						.addComponent(textAmount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(24)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblProductName)
						.addComponent(textProductName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnProductSearch)
						.addComponent(btnAddProduct))
					.addGap(18)
					.addComponent(lblCustomer, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblName)
						.addComponent(textCustomerName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAddress)
						.addComponent(textCustomerAdress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblZipCode)
						.addComponent(textCustomerZip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCity)
						.addComponent(textCustomerCity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPhoneNumber)
						.addComponent(textCustomerPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_Sales.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEmail)
						.addComponent(textCustomerEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(15)
					.addComponent(btnAddCustomer)
					.addContainerGap())
		);
		Sales.setLayout(gl_Sales);
		JPanel Products = new JPanel();
		tabbedPane.addTab("Products", null, Products, null);
		tabbedPane.setEnabledAt(1, true);
		
		model = new DefaultTableModel(col,40); 
        table=new JTable(model){@Override
        public boolean isCellEditable(int arg0, int arg1) {
         
            return false;
        }};
    JScrollPane pane = new JScrollPane(table);
    pane.setPreferredSize(table.getPreferredSize());
    productCtr.retrieveProducts();
		CreateTable();
        
    JButton btnDeleteProduct = new JButton("Delete Product");
    
    JButton btnCreateProduct = new JButton("Create Product");
    btnCreateProduct.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) 
    	{    		  		
    		new CreateProduct();
    	}
    	}
    );
    table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
   Products.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
   Products.add(btnCreateProduct);
   Products.add(btnDeleteProduct);
   
   JButton btnUpdate = new JButton("Update");
   btnUpdate.addActionListener(new ActionListener() {
   	public void actionPerformed(ActionEvent e) 
   	{
   		productCtr.retrieveProducts();
   		CreateTable();
   		
   	}
   });
   
   Products.add(btnUpdate);
   Products.add(pane);

	pack();
		
	}
	
	private void CreateTable()
	{		
		int creating = 0;
		int index = 0;
		ArrayList<Product> products = productCtr.retrieveProducts();
		while(index < products.size())
		{
			
			while(creating < products.size())
			{
	    	table.setValueAt(products.get(creating).getProductID(), creating, 0);
	    	table.setValueAt(products.get(creating).getName(), creating, 1);
	    	table.setValueAt(products.get(creating).getSalesPrice(),creating,2);
	    	table.setValueAt(products.get(creating).getStock(),creating,3);
	    	table.setValueAt(products.get(creating).getCountryOfOrigin(),creating,4);
	    	table.setValueAt(products.get(creating).getRentPrice(),creating,5);
	    	table.setValueAt(products.get(creating).getPurchasePrice(),creating,6);
	    	table.setValueAt(products.get(creating).getMinStock(),creating,7);
	    	creating++;
			}
			index++;
		}
	    	
	}
	}
